#!/usr/bin/env python

from pprint import pprint

def lenlines(hexanum):
    return 7 if hexanum in (1,2) else 6

hexas = []

HEXANUM, TITLE, STRU, EXPL, LINES = 'HEXANUM TITLE STRU EXPL LINES'.split()
LINEORD = 'first second third fourth fifth sixth all'.split()
LINENAMES = {6: 'topmost'}

NO_NAME_IN_EXPL = set([52])

state = HEXANUM

hexanum = 1
lincount = 0
hexa = {}
maxlin = 0
for lin in open('legge-data.txt'):
    lin = lin.rstrip()
    print hexanum, state
    if not lin: continue
    if state == HEXANUM:
        assert hexanum == int(lin, 10)
        maxlin = lenlines(hexanum)
        hexa['hexanum'] = hexanum
        state = TITLE
    elif state == TITLE:
        roman, title = lin.split('.', 1)
        title = title.strip().title()
        name = title.replace('The', '').replace('Hexagram', '').strip()
        hexa['name'] = name
        state = STRU
    elif state == STRU:
        stru, title2 = lin.split(' ', 1)
        hexa['stru'] = stru
        state = EXPL
    elif state == EXPL:
        if hexanum not in NO_NAME_IN_EXPL:
            assert name in lin, lin
        hexa['expl'] = lin
        state = LINES
    elif state == LINES:
        lincount += 1
        linnum, lintext = lin.split('.', 1)
        assert int(linnum) == lincount
        assert lincount <= maxlin
        if LINEORD[lincount-1] not in lintext:
            if lincount not in LINENAMES or LINENAMES[lincount] not in lintext:
                raise ValueError(lin)
        hexa['line_%s'%lincount] = lintext.strip()
        if lincount == maxlin:
            hexas.append(hexa)    
            lincount = 0
            hexanum += 1
            hexa = {}
            state = HEXANUM
       
pprint(hexas)
print(len(hexas))
